package com.mayavi.action;

import java.util.List;

import org.springframework.beans.BeanUtils;

import com.mayavi.bc.IMovieBc;
import com.mayavi.bo.MovieBo;
import com.mayavi.dto.MovieDto;

public class MovieCrudAction {
	private Integer id;
	private MovieDto movieDto;
	private List<MovieBo> movies;
	private IMovieBc movieBc;
	private String action;
	
	public String execute() {
		if ("save".equals(action)) {
			return save();
		} else if ("list".equals(action)) {
			return list();
		} else if ("edit".equals(action)) {
			return edit();
		}
		return list();
	}
	
	public String list() {
		setMovies(movieBc.getAllMovies());
		return "LIST";
	}

	public String edit() {
		if (id != null && id > 0) {
			MovieBo movieBo = movieBc.getMovieById(id);
			movieDto = new MovieDto();
			if (movieBo != null) {
				BeanUtils.copyProperties(movieBo, movieDto);
			}
		}
		return "EDIT";
	}

	public String save() {
		if (movieDto.getId() == null) {
			movieDto.setCreatedById(1);
		}
		movieDto.setModifiedById(1);
		movieBc.saveMovie(movieDto);
		return "REDIRECT";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public MovieDto getMovieDto() {
		return movieDto;
	}

	public void setMovieDto(MovieDto movieDto) {
		this.movieDto = movieDto;
	}

	public List<MovieBo> getMovies() {
		return movies;
	}

	public void setMovies(List<MovieBo> movies) {
		this.movies = movies;
	}

	public IMovieBc getMovieBc() {
		return movieBc;
	}

	public void setMovieBc(IMovieBc movieBc) {
		this.movieBc = movieBc;
	}
	
}
